//
//  LocationManager.m
//  DogeLegs
//
//  Created by 宋华 on 2017/8/20.
//  Copyright © 2017年 宋华. All rights reserved.
//

#import "LocationManager.h"
#import <CoreLocation/CoreLocation.h>

@interface LocationManager () <CLLocationManagerDelegate> {


}

@property(nonatomic,strong)CLLocationManager *locationManager;

@property(nonatomic,strong)CLGeocoder *geocoder;


@end


@implementation LocationManager

+ (LocationManager *)sharedInstance
{
    static dispatch_once_t onceBasicToken;
    
    static LocationManager *manager = nil;

    dispatch_once(&onceBasicToken, ^{
        manager = [[LocationManager alloc] init];
    });
    return manager;
}

- (id)init {
    if (self = [super init]) {
        [self configLocationManager];
    }
    return self;
}



#pragma mark -定位方法
/*
 *开始定位
 */
- (void)startLocation {
    //启用定位服务；
    [self.locationManager startUpdatingLocation];
}

/*
 *停止定位
 */
- (void)stopLocation {
    [self.locationManager stopUpdatingLocation];
}

- (void)configLocationManager {
    if (![CLLocationManager locationServicesEnabled]) {
        Log(@"用户服务不可用。");
    }
    //如果没有授权，则请求用户授权；
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
        [self.locationManager requestWhenInUseAuthorization];
    }else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse){
      
    }
}

#pragma mark - delegate :CLLocationManagerDelegate
/*
 定位协议代理方法：
 manager：当前使用的定位对象；
 locations：返回定位数据，是一个数据对象，里面是CLLocation类型；
 
 */
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    Log(@"%@",locations);
    //从数组中取出一个定位信息；
    CLLocation *location = [locations lastObject];
    //从CLlocation中取出坐标；
    //CLLocationCoordinate2D:经纬度，表示坐标系；
    
    CLLocationCoordinate2D coordinate = location.coordinate;
    Log(@"纬度:%f,经度:%f,j精度：%f,行走速度：%f",coordinate.latitude,coordinate.longitude,location.horizontalAccuracy,location.speed);
    
    if (location.horizontalAccuracy < 100) {//小于100m
        [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            if (!error && placemarks.count > 0) {
                CLPlacemark *placemark = [placemarks firstObject];
                NSString *los = [NSString stringWithFormat:@"纬度:%f,经度:%f,地址 %@",coordinate.latitude,coordinate.longitude,placemark.name];
                [NSNotificationCenter postUserAddressNF:@{UserAddress:placemark.name}];
            }

//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:los delegate:self cancelButtonTitle:nil otherButtonTitles:@"知道了", nil];
//            [alert show];
        }];
    }
}

#pragma mark - getter
- (CLLocationManager *)locationManager {
    if (_locationManager == nil) {
        _locationManager = [[CLLocationManager alloc] init];
        //设置代理；
        _locationManager.delegate = self;
        //设置精度；定位精度越高越耗电；
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        //设置频度，没隔多少米定位一次；
        CLLocationDistance distance = 10.0;
        _locationManager.distanceFilter = distance;
    }
    return _locationManager;
}

- (CLGeocoder *)geocoder {
    if (_geocoder == nil) {
        _geocoder = [[CLGeocoder alloc] init];
    }
    return _geocoder;
}

@end
